<template>
  <div >
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon
        name="arrow-left"
        color="white"
        size="0.48rem"
        class="goback"
        @click="$router.back()"
      />
      <!-- 搜索组件 -->
      <van-search
        placeholder="请输入搜索关键词"
        background="#007BFF"
        shape="round"
        v-inp
        @input="inputFn"
        v-model="kw"
        @keydown.enter="searchFn"
      />
    </div>
    <!-- 搜索建议列表 -->
    <div v-if="suggShow">
      <div
        class="sugg-list"
        v-for="(item, index) in proposalSearch"
        :key="index"
      >
        <div class="sugg-item" v-html="toRed(item, kw)" @click="chooseFn(item)">
          {{ item }}
        </div>
      </div>
    </div>
    <!-- 搜索历史 -->
    <div class="search-history" v-else>
      <!-- 标题 -->
      <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template #right-icon>
          <van-icon name="delete" class="search-icon" @click="delHistory" />
        </template>
      </van-cell>

      <!-- 历史列表 -->
      <div class="history-list">
        <span
          class="history-item"
          v-for="(item, index) in history"
          :key="index"
          @click="historySearch(item)"
          >{{ item }}</span
        >
      </div>
    </div>
  </div>
</template>

<script>
import { associativeSearch } from '@/api'
import { toRed } from '@/utils/strRed'
import { getToken, setToken, removeToken } from '@/utils/token'
export default {
  data () {
    return {
      kw: '', // 搜索关键字
      timer: null,
      proposalSearch: [], // 建议搜索列表
      history: [], // 搜索历史
      suggShow: false
    }
  },
  created () {
    this.history = JSON.parse(getToken('history')) || []
    // document.addEventListener('keydown', e => {
    //   if (e.keyCode === 13) {
    //     this.searchFn()
    //   }
    // })
  },
  methods: {
    inputFn () {
      // 防抖
      clearTimeout(this.timer)
      this.timer = setTimeout(async () => {
        if (!this.kw) {
          this.proposalSearch = []
          this.suggShow = false
          return
        }
        const res = await associativeSearch(this.kw)
        this.proposalSearch = res.data.data.options[0]
          ? res.data.data.options
          : []
      }, 500)
      this.suggShow = true
    },
    // 关键字高亮
    toRed,
    chooseFn (item) {
      this.kw = item
      this.searchFn()
    },
    historySearch (item) {
      this.jumpFn(item)
    },
    // 搜索事件
    searchFn () {
      if (this.kw) {
        this.history.push(this.kw)
        this.history = [...new Set(this.history)]
        setToken('history', JSON.stringify(this.history))
        this.jumpFn()
      }
    },
    // 跳转到搜索页面
    jumpFn (str) {
      this.$router.push({
        path: '/search-result',
        query: {
          kw: str || this.kw
        }
      })
      this.kw = ''
      this.suggShow = false
    },
    // 删除历史
    delHistory () {
      this.history = []
      removeToken('history')
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
/*搜索建议列表样式 */
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
/**搜索历史 */
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
